package impl;

import Entity.DeviceInfo;
import util.MySQLConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeviceOpt {

    /***
     *  增加设备
     * @param device 设备的信息
     */
    public   void add(DeviceInfo device) throws SQLException {
        String strSQL ="insert into device(id,name,code,state,sort,position) values (?,?,?,?,?,?)";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setInt(1,device.getId());
        preparedStatement.setString(2,device.getName());
        preparedStatement.setString(3,device.getCode());
        preparedStatement.setString(4,device.getState());
        preparedStatement.setInt(5,device.getSort());
        preparedStatement.setString(6,device.getPosition());
        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("插入失败");
        }else {
            System.out.println("插入成功"+i);
        }

    }


    /***
     *  删除设备功能
     * @param device 插入一条设备信息
     */
    public  void delete(DeviceInfo device) throws SQLException {

        String strSQL =" delete  from  device where id = ? ";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setInt(1,device.getId());

        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("更新失败");
        }else {
            System.out.println("更新成功"+i);
        }
    }


    /***
     * 更新数据
     * @param device
     */
    public  void update(DeviceInfo device) throws SQLException {
        String strSQL ="update  device  set name =?,code=?,state=?,sort=?,position=? where id =?";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        preparedStatement.setString(1,device.getName());
        preparedStatement.setString(2,device.getCode());
        preparedStatement.setString(3,device.getState());
        preparedStatement.setInt(4,device.getSort());
        preparedStatement.setString(5,device.getPosition());
        preparedStatement.setInt(6,device.getId());


        int i = preparedStatement.executeUpdate();//执行 上面的语句，比如插入一条，告诉我们影响了一条
        if(i==0){
            System.out.println("更新失败");
        }else {
            System.out.println("更新成功"+i);
        }

    }

    /***
     * 查询数据
     * @param device
     */
    public  void search(DeviceInfo device) throws SQLException {

        String strSQL =" select * from device";
        Connection connection = MySQLConnection.getConnection();
        PreparedStatement preparedStatement= connection.prepareStatement(strSQL);
        ResultSet resultSet = preparedStatement.executeQuery(); //执行之后返回的结果
        while(resultSet.next()){ //通过迭代器循环获取所有的值。
            System.out.println(resultSet.getString("id")+"-"+resultSet.getString("name")+"-"+resultSet.getString("code")+"-"+resultSet.getString("state")+"-"+resultSet.getString("sort")+"-"+resultSet.getString("position"));
        }
    }
}
